package com.bff.gaia.streaming.api.mo.gaia.io.basiccoder;

import java.io.*;

public class DoubleCoder {
	private Double val;
	private byte[] data;

	public DoubleCoder(Double val) {
		this.val = val;
		this.makeBytes();
	}

	public DoubleCoder(byte[] data) {
		this.data = data;
		this.makeDouble();
	}
	private void makeDouble() {
		DataInputStream din = new DataInputStream(new ByteArrayInputStream(this.data));

		try {
			int flag = din.readInt();
			if (flag == 0) {
				this.val = null;
			} else {
				double val1 = din.readDouble();
				this.val = new Double(val1);
			}
		} catch (IOException var5) {
			System.out.println("this is not the normal data");
			var5.printStackTrace();
		}

	}
	private void makeBytes() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(bos);
		if (this.val == null) {
			try {
				dataOutputStream.writeInt(0);
				dataOutputStream.writeDouble(0.0D);
			} catch (IOException var5) {
				var5.printStackTrace();
				this.data = null;
			}
		} else {
			try {
				dataOutputStream.writeInt(1);
				dataOutputStream.writeDouble(this.val);
			} catch (IOException var4) {
				var4.printStackTrace();
				this.data = null;
			}
		}

		this.data = bos.toByteArray();
	}
	public Double getDouble() {
		return this.val;
	}
	public byte[] getBytes() {
		return this.data;
	}
}